package com.qk.service.impl;

import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private RedisTemplate<Object ,Object> redisTemplate;
    @Override
    public OverviewVo getOverview() {
        log.info("从Redis中，查询线索和商机的概览数据");
        Object overviewData = redisTemplate.opsForValue().get("OVERVIEW_DATA");
        if(overviewData != null){
            log.info("从Redis中，查询线索和商机的概览数据成功");
            return (OverviewVo) overviewData;
        }
        //缓存中没有，从数据库中查询
        OverviewVo ClueOverview =clueMapper.getClueOverview();
        OverviewVo BusinessOverview =businessMapper.getBusinessOverview();
        BeanUtils.copyProperties(ClueOverview,BusinessOverview,"businessTotal","businessWaitAllot","businessWaitFollow","businessFollowing","businessFalse","businessConvertCustomer");

        redisTemplate.opsForValue().set("OVERVIEW_DATA",BusinessOverview,60, TimeUnit.SECONDS);
        return BusinessOverview;
    }
}
